﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Reflection;

namespace Dracham.MapEditor.Zones.Events
{
    public abstract class Event
    {
        public List<Actions.Action> Actions { get; private set; }

        public Event()
        {
            this.Actions = new List<Actions.Action>();
        }

        protected void ParseActions(XmlNode node)
        {
            this.Actions = new List<Dracham.MapEditor.Zones.Events.Actions.Action>();

            Assembly a = typeof(Actions.Action).Assembly;
            String typeFullNamePrefix = typeof(Actions.Action).FullName;
            typeFullNamePrefix = typeFullNamePrefix.Substring(0, typeFullNamePrefix.LastIndexOf(".") + 1);

            foreach (XmlNode child in node.ChildNodes)
            {
                Type t = a.GetType(typeFullNamePrefix + child.Name);

                if (t != null && t.BaseType == typeof(Actions.Action))
                {
                    MethodInfo mi = t.GetMethod("FromXmlNode", BindingFlags.Static | BindingFlags.Public);

                    if (mi != null)
                    {
                        this.Actions.Add((Actions.Action)mi.Invoke(null, new Object[] { child }));
                    }
                }
            }
        }

        public abstract XmlNode ToXmlNode();
    }
}
